package dfsketches.gr3d;

import java.util.HashMap;
import java.util.Vector;


public class ModelConstructor {

	private Bone mainBone;
	private Vector<Triangle> triangles;
	private HashMap<String, Animation> animCollection;
	private HashMap<String, Bone> refs;
	
	public ModelConstructor() {
		triangles = new Vector<Triangle>();
		animCollection = new HashMap<String, Animation>();
		refs = new HashMap<String, Bone>();
	}
	
	/* 1-----2
	   |     |
	   3-----4
	 */
	public void addRectangle(int color, Vertex... corners) {
		addTriangle(color, corners[0], corners[1], corners[2]);
		addTriangle(color, corners[1], corners[2], corners[3]);
	}
	
	/*   3-----4
	    /     /|
	   1-----2 8
	   |     |/
	   5-----6
	 */
	public void addBox(int color, Vertex... corners) {
		addRectangle(color, corners[0], corners[1], corners[2], corners[3]);
		addRectangle(color, corners[4], corners[5], corners[6], corners[7]);
		addRectangle(color, corners[0], corners[1], corners[4], corners[5]);
		addRectangle(color, corners[2], corners[3], corners[6], corners[7]);
		addRectangle(color, corners[0], corners[2], corners[4], corners[6]);
		addRectangle(color, corners[1], corners[3], corners[5], corners[7]);
	}
	
	public void addTriangle(int color, Vertex... v) {
		triangles.add(new Triangle(v[0], v[1], v[2], color));
	}
	
	public void setMainBone(Bone b) {
		mainBone = b;
	}
	
	public void apply(Matrix3D m) {
		mainBone.apply(m);
	}

	public Model construct() {
		Vector<Vertex> vertices = new Vector<Vertex>();
		mainBone.verticesToCollection(vertices);

		Model m = new Model();
		m.mainBone = mainBone;
		
		m.animCollection = new HashMap<String, Animation>(animCollection);
		m.animCollection.put("none", new NoAnimation());
		
		m.triangles = new Triangle[triangles.size()];
		int j = 0;
		for (Triangle t : triangles) {
			m.triangles[j++] = t;
		}
		
		m.refs = new HashMap<String, Bone>(refs);
		
		return m;
	}

	public void addAnimation(String name, Animation animation) {
		animCollection.put(name, animation);
	}
	
	public void addReference(String name, Bone anchor) {
		refs.put(name, anchor);
	}

}